package cn.xpf.study.controller;

import cn.xpf.study.dao.LessonMapper;
import cn.xpf.study.dao.LessonVideoMapper;
import cn.xpf.study.model.Lesson;
import cn.xpf.study.model.LessonVideo;
import cn.xpf.study.model.vo.ResourceVo;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Create on 2021/5/13
 *
 * @author shuxinsheng@qiyi.com
 **/
@RequestMapping("/resource")
@Controller
public class ResourceController {

    @Autowired
    LessonMapper lessonMapper;

    @Autowired
    LessonVideoMapper lessonVideoMapper;

//    @RequestMapping("/index")
//    public String index() {
//        return "resource";
//    }

    @RequestMapping("/index")
    public String list(Model model) {
        List<Lesson> lessonList = lessonMapper.findAll();
        List<ResourceVo>  resourceVoList = new ArrayList<>();
        if (lessonList != null) {
            for (Lesson lesson: lessonList) {
                int lessonId = lesson.id;
                List<LessonVideo>  lessonVideos = lessonVideoMapper.findAllByLessonId(lessonId);
                if (lessonVideos != null && lessonVideos.size() > 0) {
                    ResourceVo resourceVo = new ResourceVo();
                    resourceVo.setLessonId(lessonId);
                    resourceVo.setName(lesson.name);
                    resourceVo.setTitle(lesson.title);
                    resourceVo.setVideoList(lessonVideos);
                    resourceVoList.add(resourceVo);
                }
            }
        }
        model.addAttribute("resourceVoList", resourceVoList);
        return "resource_demo";
    }

}
